package com.ciccwm.core.util;

import java.util.Random;

/**
 * 字符串工具类.
 *
 * @author ciccwm
 */
public final class StringUtils {
    private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final Random RANDOM = new Random();

    private StringUtils() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }

    /**
     * 判断字符串是否为空.
     *
     * @param str 字符串
     * @return 是否为空
     */
    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    /**
     * 判断字符串是否不为空.
     *
     * @param str 字符串
     * @return 是否不为空
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 生成指定长度的随机字符串.
     *
     * @param length 长度
     * @return 随机字符串
     */
    public static String randomString(int length) {
        if (length <= 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder(length);
        for (int index = 0; index < length; index++) {
            sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
        }
        return sb.toString();
    }

    /**
     * 将驼峰命名转换为下划线命名.
     *
     * @param str 驼峰命名字符串
     * @return 下划线命名字符串
     */
    public static String camelToUnderline(String str) {
        if (isEmpty(str)) {
            return str;
        }

        StringBuilder sb = new StringBuilder();
        for (int index = 0; index < str.length(); index++) {
            char currentChar = str.charAt(index);
            if (Character.isUpperCase(currentChar)) {
                if (index > 0) {
                    sb.append('_');
                }
                sb.append(Character.toLowerCase(currentChar));
            } else {
                sb.append(currentChar);
            }
        }
        return sb.toString();
    }

    /**
     * 将下划线命名转换为驼峰命名.
     *
     * @param str 下划线命名字符串
     * @return 驼峰命名字符串
     */
    public static String underlineToCamel(String str) {
        if (isEmpty(str)) {
            return str;
        }

        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int index = 0; index < str.length(); index++) {
            char currentChar = str.charAt(index);
            if (currentChar == '_') {
                upperCase = true;
            } else {
                sb.append(upperCase ? Character.toUpperCase(currentChar) : currentChar);
                upperCase = false;
            }
        }
        return sb.toString();
    }

    /**
     * 截取字符串，支持中文.
     *
     * @param str 字符串
     * @param maxLength 最大长度
     * @return 截取后的字符串
     */
    public static String truncate(String str, int maxLength) {
        if (isEmpty(str) || maxLength <= 0) {
            return str;
        }

        if (str.length() <= maxLength) {
            return str;
        }

        return str.substring(0, maxLength) + "...";
    }

    /**
     * 隐藏字符串中间部分.
     *
     * @param str 字符串
     * @param startLength 开始长度
     * @param endLength 结束长度
     * @return 隐藏后的字符串
     */
    public static String mask(String str, int startLength, int endLength) {
        if (isEmpty(str)) {
            return str;
        }

        if (startLength < 0) {
            startLength = 0;
        }

        if (endLength < 0) {
            endLength = 0;
        }

        int length = str.length();
        if (startLength + endLength >= length) {
            return str;
        }

        String start = str.substring(0, startLength);
        String end = str.substring(length - endLength);
        return start + "*".repeat(length - startLength - endLength) + end;
    }
} 
